from rest_framework import serializers

from .models import Goods,GoodsCategory


class CategorySerializer3(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        # 如果需要序列化所有字段
        fields = "__all__"

class CategorySerializer2(serializers.ModelSerializer):
    # 此处一定需要增加many = True
    sub_cat = CategorySerializer3(many=True)
    class Meta:
        model = GoodsCategory
        # 如果需要序列化所有字段
        fields = "__all__"

class CategorySerializer(serializers.ModelSerializer):
    # 此处一定需要增加many = True
    sub_cat = CategorySerializer2(many=True)
    class Meta:
        model = GoodsCategory
        # 如果需要序列化所有字段
        fields = "__all__"

# 在对应的Goods中进行实例化即可
class GoodsSerializer(serializers.ModelSerializer):
    category = CategorySerializer()
    class Meta:
        model = Goods
        # 如果需要序列化所有字段
        fields = "__all__"